home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 6 / CU Amiga Magazine's Super CD-ROM 06 (1996)(EMAP Images)(GB)(Track 1 of 4)[!][issue 1997-01].iso / cucd / prog / gnu-c / src / gcc-2.7.0-amiga / cp / sig.c < prev    next >
C/C++ Source or Header  |  1995-06-15  |  32KB  |  1,028 lines

  1. /* Functions dealing with signatures and signature pointers/references.
  2.    Copyright (C) 1992, 1995 Free Software Foundation, Inc.
  3.    Contributed by Gerald Baumgartner (gb@cs.purdue.edu)
  4.  
  5. This file is part of GNU CC.
  6.  
  7. GNU CC is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2, or (at your option)
  10. any later version.
  11.  
  12. GNU CC is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with GNU CC; see the file COPYING.  If not, write to
  19. the Free Software Foundation, 59 Temple Place - Suite 330,
  20. Boston, MA 02111-1307, USA.  */
  21.  
  22.  
  23. #include "config.h"
  24. #include <stdio.h>
  25. #include "obstack.h"
  26. #include "tree.h"
  27. #include "cp-tree.h"
  28. #include "flags.h"
  29. #include "assert.h"
  30.  
  31. extern struct obstack *current_obstack;
  32. extern struct obstack permanent_obstack;
  33. extern struct obstack *saveable_obstack;
  34.  
  35. extern void error ();
  36. extern void sorry ();
  37. extern void compiler_error ();
  38. extern void make_decl_rtl            PROTO((tree, char *, int));
  39.  
  40. /* Used to help generate globally unique names for signature tables.  */
  41.  
  42. static int global_sigtable_name_counter;
  43.  
  44. /* Build an identifier for a signature pointer or reference, so we
  45.    can use it's name in function name mangling.  */
  46.  
  47. static tree
  48. build_signature_pointer_or_reference_name (to_type, constp, volatilep, refp)
  49.      tree to_type;
  50.      int constp, volatilep, refp;
  51. {
  52.   char * sig_name = TYPE_NAME_STRING (to_type);
  53.   int name_len = TYPE_NAME_LENGTH (to_type) + constp + volatilep;
  54.   char * name;
  55.  
  56.   if (refp)
  57.     {
  58.       name = (char *) alloca (name_len + sizeof (SIGNATURE_REFERENCE_NAME) +2);
  59.       sprintf (name, SIGNATURE_REFERENCE_NAME_FORMAT,
  60.            constp ? "C" : "", volatilep ? "V": "", sig_name);
  61.     }
  62.   else
  63.     {
  64.       name = (char *) alloca (name_len + sizeof (SIGNATURE_POINTER_NAME) + 2);
  65.       sprintf (name, SIGNATURE_POINTER_NAME_FORMAT,
  66.            constp ? "C" : "", volatilep ? "V": "", sig_name);
  67.     }
  68.   return get_identifier (name);
  69. }
  70.  
  71. /* Build a DECL node for a signature pointer or reference, so we can
  72.    tell the debugger the structure of signature pointers/references.
  73.    This function is called at most eight times for a given signature,
  74.    once for each [const] [volatile] signature pointer/reference.  */
  75.  
  76. static void
  77. build_signature_pointer_or_reference_decl (type, name)
  78.      tree type, name;
  79. {
  80.   tree decl;
  81.  
  82.   /* We don't enter this declaration in any sort of symbol table.  */
  83.   decl = build_decl (TYPE_DECL, name, type);
  84.   TYPE_NAME (type) = decl;
  85.   TREE_CHAIN (type) = decl;
  86. }
  87.  
  88. /* Construct, lay out and return the type of pointers or references
  89.    to signature TO_TYPE.  If such a type has already been constructed,
  90.    reuse it. If CONSTP or VOLATILEP is specified, make the `optr' const
  91.    or volatile, respectively.   If we are constructing a const/volatile
  92.    type variant and the main type variant doesn't exist yet, it is built
  93.    as well.  If REFP is 1, we construct a signature reference, otherwise
  94.    a signature pointer is constructed.
  95.  
  96.    This function is a subroutine of `build_signature_pointer_type' and
  97.    `build_signature_reference_type'.  */
  98.  
  99. static tree
  100. build_signature_pointer_or_reference_type (to_type, constp, volatilep, refp)
  101.      tree to_type;
  102.      int constp, volatilep, refp;
  103. {
  104.   register tree t, m;
  105.   register struct obstack *ambient_obstack = current_obstack;
  106.   register struct obstack *ambient_saveable_obstack = saveable_obstack;
  107.  
  108.   m = refp ? SIGNATURE_REFERENCE_TO (to_type) : SIGNATURE_POINTER_TO (to_type);
  109.  
  110.   /* If we don't have the main variant yet, construct it.  */
  111.   if (m == NULL_TREE
  112.       && (constp || volatilep))
  113.     m = build_signature_pointer_or_reference_type (to_type, 0, 0, refp);
  114.  
  115.   /* Treat any nonzero argument as 1.  */
  116.   constp = !!constp;
  117.   volatilep = !!volatilep;
  118.   refp = !!refp;
  119.  
  120.   /* If not generating auxiliary info, search the chain of variants to see
  121.      if there is already one there just like the one we need to have.  If so,
  122.      use that existing one.
  123.  
  124.      We don't do this in the case where we are generating aux info because
  125.      in that case we want each typedef names to get it's own distinct type
  126.      node, even if the type of this new typedef is the same as some other
  127.      (existing) type.  */
  128.  
  129.   if (m && !flag_gen_aux_info)
  130.     for (t = m; t; t = TYPE_NEXT_VARIANT (t))
  131.       if (constp == TYPE_READONLY (TREE_TYPE (TREE_TYPE (TYPE_FIELDS (t))))
  132.       && volatilep == TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (TYPE_FIELDS (t)))))
  133.         return t;
  134.  
  135.   /* We need a new one.  If TO_TYPE is permanent, make this permanent too.  */
  136.   if (TREE_PERMANENT (to_type))
  137.     {
  138.       current_obstack = &permanent_obstack;
  139.       saveable_obstack = &permanent_obstack;
  140.     }
  141.  
  142.   /* A signature pointer or reference to a signature `s' looks like this:
  143.  
  144.        struct {
  145.          void * optr;
  146.      const s * sptr;
  147.      vtbl_type_node * vptr;
  148.        };
  149.  
  150.      A `const' signature pointer/reference is a
  151.  
  152.        struct {
  153.          const void * optr;
  154.      const s * sptr;
  155.      vtbl_type_node * vptr;
  156.        };
  157.  
  158.      Similarly, for `volatile' and `const volatile'.
  159.    */
  160.  
  161.   t = make_lang_type (RECORD_TYPE);
  162.   {
  163.     tree obj_type = build_type_variant (void_type_node, constp, volatilep);
  164.     tree optr_type = build_pointer_type (obj_type);
  165.     tree optr, sptr, vptr;
  166.  
  167.     optr = build_lang_field_decl (FIELD_DECL,
  168.                   get_identifier (SIGNATURE_OPTR_NAME),
  169.                   optr_type);
  170.     DECL_FIELD_CONTEXT (optr) = t;
  171.     DECL_CLASS_CONTEXT (optr) = t;
  172.  
  173.     if (m)
  174.       {
  175.     /* We can share `sptr' and `vptr' among type variants.  */
  176.     sptr = TREE_CHAIN (TYPE_FIELDS (m));
  177.     vptr = TREE_CHAIN (sptr);
  178.       }
  179.     else
  180.       {
  181.     tree sig_tbl_type = cp_build_type_variant (to_type, 1, 0);
  182.     
  183.     sptr = build_lang_field_decl (FIELD_DECL,
  184.                       get_identifier (SIGNATURE_SPTR_NAME),
  185.                       build_pointer_type (sig_tbl_type));
  186.     vptr = build_lang_field_decl (FIELD_DECL,
  187.                       get_identifier (SIGNATURE_VPTR_NAME),
  188.                       build_pointer_type (vtbl_type_node));
  189.     DECL_FIELD_CONTEXT (sptr) = t;
  190.     DECL_CLASS_CONTEXT (sptr) = t;
  191.     DECL_FIELD_CONTEXT (vptr) = t;
  192.     DECL_CLASS_CONTEXT (vptr) = t;
  193.     TREE_CHAIN (sptr) = vptr;
  194.     TREE_CHAIN (vptr) = NULL_TREE;
  195.       }
  196.  
  197.     TREE_CHAIN (optr) = sptr;
  198.     TYPE_FIELDS (t) = optr;
  199.     /* To make `build_vfn_ref' work when building a signature method call.  */
  200.     CLASSTYPE_VFIELD (t) = vptr;
  201.     DECL_FCONTEXT (CLASSTYPE_VFIELD (t)) = t;
  202.     TYPE_ALIGN (t) = TYPE_ALIGN (optr_type);
  203.  
  204.     /* A signature pointer/reference isn't a `real' class.  */
  205.     IS_AGGR_TYPE (t) = 0;
  206.   }
  207.  
  208.   {
  209.     tree name = build_signature_pointer_or_reference_name (to_type, constp,
  210.                                volatilep, refp);
  211.  
  212.     /* Build a DECL node for this type, so the debugger has access to it.  */
  213.     build_signature_pointer_or_reference_decl (t, name);
  214.   }
  215.  
  216.   CLASSTYPE_GOT_SEMICOLON (t) = 1;
  217.   IS_SIGNATURE_POINTER (t) = ! refp;
  218.   IS_SIGNATURE_REFERENCE (t) = refp;
  219.   SIGNATURE_TYPE (t) = to_type;
  220.  
  221.   if (m)
  222.     {
  223.       /* Add this type to the chain of variants of TYPE.
  224.      Every type has to be its own TYPE_MAIN_VARIANT.  */
  225.       TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
  226.       TYPE_NEXT_VARIANT (m) = t;
  227.     }
  228.   else if (refp)
  229.     /* Record this type as the reference to TO_TYPE.  */
  230.     SIGNATURE_REFERENCE_TO (to_type) = t;
  231.   else
  232.     /* Record this type as the pointer to TO_TYPE.  */
  233.     SIGNATURE_POINTER_TO (to_type) = t;
  234.  
  235.   /* Lay out the type.  This function has many callers that are concerned
  236.      with expression-construction, and this simplifies them all.
  237.      Also, it guarantees the TYPE_SIZE is permanent if the type is.  */
  238.   layout_type (t);
  239.  
  240.   current_obstack = ambient_obstack;
  241.   saveable_obstack = ambient_saveable_obstack;
  242.  
  243.   /* Output debug information for this type.  */
  244.   rest_of_type_compilation (t, 1);
  245.  
  246.   return t;
  247. }
  248.  
  249. /* Construct, lay out and return the type of pointers to signature TO_TYPE.  */
  250.  
  251. tree
  252. build_signature_pointer_type (to_type, constp, volatilep)
  253.      tree to_type;
  254.      int constp, volatilep;
  255. {
  256.   return
  257.     build_signature_pointer_or_reference_type (to_type, constp, volatilep, 0);
  258. }
  259.  
  260. /* Construct, lay out and return the type of pointers to signature TO_TYPE.  */
  261.  
  262. tree
  263. build_signature_reference_type (to_type, constp, volatilep)
  264.      tree to_type;
  265.      int constp, volatilep;
  266. {
  267.   return
  268.     build_signature_pointer_or_reference_type (to_type, constp, volatilep, 1);
  269. }
  270.  
  271. /* Return the name of the signature table (as an IDENTIFIER_NODE)
  272.    for the given signature type SIG_TYPE and rhs type RHS_TYPE.  */
  273.  
  274. static tree
  275. get_sigtable_name (sig_type, rhs_type)
  276.      tree sig_type, rhs_type;
  277. {
  278.   tree sig_type_id = build_typename_overload (sig_type);
  279.   tree rhs_type_id = build_typename_overload (rhs_type);
  280.   char *buf = (char *) alloca (sizeof (SIGTABLE_NAME_FORMAT_LONG)
  281.                    + IDENTIFIER_LENGTH (sig_type_id)
  282.                    + IDENTIFIER_LENGTH (rhs_type_id) + 20);
  283.   char *sig_ptr = IDENTIFIER_POINTER (sig_type_id);
  284.   char *rhs_ptr = IDENTIFIER_POINTER (rhs_type_id);
  285.   int i, j;
  286.  
  287.   for (i = 0; sig_ptr[i] == OPERATOR_TYPENAME_FORMAT[i]; i++)
  288.     /* do nothing */;
  289.   while (sig_ptr[i] >= '0' && sig_ptr[i] <= '9')
  290.     i += 1;
  291.  
  292.   for (j = 0; rhs_ptr[j] == OPERATOR_TYPENAME_FORMAT[j]; j++)
  293.     /* do nothing */;
  294.   while (rhs_ptr[j] >= '0' && rhs_ptr[j] <= '9')
  295.     j += 1;
  296.  
  297.   if (IS_SIGNATURE (rhs_type))
  298.     sprintf (buf, SIGTABLE_NAME_FORMAT_LONG, sig_ptr+i, rhs_ptr+j,
  299.          global_sigtable_name_counter++);
  300.   else
  301.     sprintf (buf, SIGTABLE_NAME_FORMAT, sig_ptr+i, rhs_ptr+j);
  302.   return get_identifier (buf);
  303. }
  304.  
  305. /* Build a field decl that points to a signature member function.  */
  306.  
  307. static tree
  308. build_member_function_pointer (member)
  309.      tree member;
  310. {
  311.   char *namstr = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (member));
  312.   int namlen = IDENTIFIER_LENGTH (DECL_ASSEMBLER_NAME (member));
  313.   char *name;
  314.   tree entry;
  315.   
  316.   name = (char *) alloca (namlen + sizeof (SIGNATURE_FIELD_NAME) + 2);
  317.   sprintf (name, SIGNATURE_FIELD_NAME_FORMAT, namstr);
  318.  
  319.   /* @@ Do we really want to xref signature table fields?  */
  320.   GNU_xref_ref (current_function_decl, name);
  321.  
  322.   entry = build_lang_field_decl (FIELD_DECL, get_identifier (name),
  323.                  TYPE_MAIN_VARIANT (sigtable_entry_type));
  324.   TREE_CONSTANT (entry) = 1;
  325.   TREE_READONLY (entry) = 1;
  326.  
  327.   /* @@ Do we really want to xref signature table fields?  */
  328.   GNU_xref_decl (current_function_decl, entry);
  329.  
  330.   return entry;
  331. }
  332.  
  333. /* For each FUNCTION_DECL in a signature we construct a member function
  334.    pointer of the appropriate type.  We also need two flags to test
  335.    whether the member function pointer points to a virtual function or
  336.    to a default implementation.  Those flags will be the two lower order
  337.    bits of the member function pointer (or the two higher order bits,
  338.    based on the configuration).
  339.  
  340.    The new FIELD_DECLs are appended at the end of the last (and only)
  341.    sublist of `list_of_fieldlists.'
  342.  
  343.    As a side effect, each member function in the signature gets the
  344.    `decl.ignored' bit turned on, so we don't output debug info for it.  */
  345.  
  346. void
  347. append_signature_fields (list_of_fieldlists)
  348.      tree list_of_fieldlists;
  349. {
  350.   tree l, x;
  351.   tree last_x = NULL_TREE;
  352.   tree mfptr;
  353.   tree last_mfptr;
  354.   tree mfptr_list = NULL_TREE;
  355.           
  356.   /* For signatures it should actually be only a list with one element.  */
  357.   for (l = list_of_fieldlists; l; l = TREE_CHAIN (l))
  358.     {
  359.       for (x = TREE_VALUE (l); x; x = TREE_CHAIN (x))
  360.     {
  361.       if (TREE_CODE (x) == FUNCTION_DECL)
  362.         {
  363.           mfptr = build_member_function_pointer (x);
  364.           DECL_MEMFUNC_POINTER_TO (x) = mfptr;
  365.           DECL_MEMFUNC_POINTING_TO (mfptr) = x;
  366.           DECL_IGNORED_P (x) = 1;
  367.           DECL_IN_AGGR_P (mfptr) = 1;
  368.           if (! mfptr_list)
  369.         mfptr_list = last_mfptr = mfptr;
  370.           else
  371.         {
  372.           TREE_CHAIN (last_mfptr) = mfptr;
  373.           last_mfptr = mfptr;
  374.         }
  375.         }
  376.       last_x = x;
  377.     }
  378.     }
  379.  
  380.   /* Append the lists.  */
  381.   if (last_x && mfptr_list)
  382.     {
  383.       TREE_CHAIN (last_x) = mfptr_list;
  384.       TREE_CHAIN (last_mfptr) = NULL_TREE;
  385.     }
  386. }
  387.  
  388. /* Compare the types of a signature member function and a class member
  389.    function.  Returns 1 if the types are in the C++ `<=' relationship.
  390.  
  391.    If we have a signature pointer/reference as argument or return type
  392.    we don't want to do a recursive conformance check.  The conformance
  393.    check only succeeds if both LHS and RHS refer to the same signature
  394.    pointer.  Otherwise we need to keep information about parameter types
  395.    around at run time to initialize the signature table correctly.  */
  396.  
  397. static int
  398. match_method_types (sig_mtype, class_mtype)
  399.      tree sig_mtype, class_mtype;
  400. {
  401.   tree sig_return_type = TREE_TYPE (sig_mtype);
  402.   tree sig_arg_types = TYPE_ARG_TYPES (sig_mtype);
  403.   tree class_return_type = TREE_TYPE (class_mtype);
  404.   tree class_arg_types = TYPE_ARG_TYPES (class_mtype);
  405.  
  406.   /* The return types have to be the same.  */
  407.   if (! comptypes (sig_return_type, class_return_type, 1))
  408.     return 0;
  409.  
  410.   /* Compare the first argument `this.'  */
  411.   {
  412.     /* Get the type of what the `optr' is pointing to.  */
  413.     tree sig_this =
  414.       TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_VALUE (sig_arg_types))));
  415.     tree class_this = TREE_VALUE (class_arg_types);
  416.  
  417.     if (TREE_CODE (class_this) == RECORD_TYPE)    /* Is `this' a sig ptr?  */
  418.       class_this = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (class_this)));
  419.     else
  420.       class_this = TREE_TYPE (class_this);
  421.  
  422.     /* If a signature method's `this' is const or volatile, so has to be
  423.        the corresponding class method's `this.'  */
  424.     if ((TYPE_READONLY (sig_this) && ! TYPE_READONLY (class_this))
  425.     || (TYPE_VOLATILE (sig_this) && ! TYPE_VOLATILE (class_this)))
  426.       return 0;
  427.   }
  428.  
  429.   sig_arg_types = TREE_CHAIN (sig_arg_types);
  430.   class_arg_types = TREE_CHAIN (class_arg_types);
  431.  
  432.   /* The number of arguments and the argument types have to be the same.  */
  433.   return compparms (sig_arg_types, class_arg_types, 3);
  434. }
  435.  
  436. /* Undo casts of opaque type variables to the RHS types.  */
  437. static void
  438. undo_casts (sig_ty)
  439.      tree sig_ty;
  440. {
  441.   tree field = TYPE_FIELDS (sig_ty);
  442.  
  443.   /* Since all the FIELD_DECLs for the signature table entries are at the end
  444.      of the chain (see `append_signature_fields'), we can do it this way.  */
  445.   for (; field && TREE_CODE (field) != FIELD_DECL; field = TREE_CHAIN (field))
  446.     if (TYPE_MAIN_VARIANT (TREE_TYPE (field)) == opaque_type_node)
  447.       TREE_TYPE (TREE_TYPE (field)) = TREE_TYPE (ptr_type_node);
  448. }
  449.  
  450. /* Do the type checking necessary to see whether the `rhs' conforms to
  451.    the lhs's `sig_ty'.  Depending on the type of `rhs' return a NULL_TREE,
  452.    an integer_zero_node, a constructor, or an expression offsetting the
  453.    `rhs' signature table.  */
  454.  
  455. static tree
  456. build_signature_table_constructor (sig_ty, rhs)
  457.      tree sig_ty, rhs;
  458. {
  459.   tree rhstype = TREE_TYPE (rhs);
  460.   tree sig_field = TYPE_FIELDS (sig_ty);
  461.   tree result = NULL_TREE;
  462.   tree first_rhs_field = NULL_TREE;
  463.   tree last_rhs_field;
  464.   int sig_ptr_p = IS_SIGNATURE (rhstype);
  465.   int offset_p = sig_ptr_p;
  466.  
  467.   rhstype = sig_ptr_p ? rhstype : TREE_TYPE (rhstype);
  468.  
  469.   if (CLASSTYPE_TAGS (sig_ty))
  470.     {
  471.       sorry ("conformance check with signature containing class declarations");
  472.       return error_mark_node;
  473.     }
  474.  
  475.   for (; sig_field; sig_field = TREE_CHAIN (sig_field))
  476.     {
  477.       tree basetype_path, baselink, basetypes;
  478.       tree sig_method, sig_mname, sig_mtype;
  479.       tree rhs_method, tbl_entry;
  480.  
  481.       if (TREE_CODE (sig_field) == TYPE_DECL)
  482.     {
  483.       tree sig_field_type = TREE_TYPE (sig_field);
  484.  
  485.       if (TYPE_MAIN_VARIANT (sig_field_type) == opaque_type_node)
  486.         {
  487.           /* We've got an opaque type here.  */
  488.           tree oty_name = DECL_NAME (sig_field);
  489.           tree oty_type = lookup_field (rhstype, oty_name, 1, 1);
  490.  
  491.           if (oty_type == NULL_TREE || oty_type == error_mark_node)
  492.         {
  493.           cp_error ("class `%T' does not contain type `%T'",
  494.                 rhstype, oty_type);
  495.           undo_casts (sig_ty);
  496.           return error_mark_node;
  497.         }
  498.           oty_type = TREE_TYPE (oty_type);
  499.  
  500.           /* Cast `sig_field' to be of type `oty_type'.  This will be
  501.          undone in `undo_casts' by walking over all the TYPE_DECLs.  */
  502.           TREE_TYPE (sig_field_type) = TREE_TYPE (oty_type);
  503.         }
  504.       /* If we don't have an opaque type, we can ignore the `typedef'.  */
  505.       continue;
  506.     }
  507.  
  508.       /* Find the signature method corresponding to `sig_field'.  */
  509.       sig_method = DECL_MEMFUNC_POINTING_TO (sig_field);
  510.       sig_mname = DECL_NAME (sig_method);
  511.       sig_mtype = TREE_TYPE (sig_method);
  512.  
  513.       basetype_path = TYPE_BINFO (rhstype);
  514.       baselink = lookup_fnfields (basetype_path, sig_mname, 0);
  515.       if (baselink == NULL_TREE || baselink == error_mark_node)
  516.     {
  517.       if (! IS_DEFAULT_IMPLEMENTATION (sig_method))
  518.         {
  519.           cp_error ("class `%T' does not contain method `%D'",
  520.             rhstype, sig_mname);
  521.           undo_casts (sig_ty);
  522.           return error_mark_node;
  523.         }
  524.       else
  525.         {
  526.           /* We use the signature's default implementation.  */
  527.           rhs_method = sig_method;
  528.         }
  529.     }
  530.       else
  531.     {
  532.       /* Find the class method of the correct type.  */
  533.  
  534.       basetypes = TREE_PURPOSE (baselink);
  535.       if (TREE_CODE (basetypes) == TREE_LIST)
  536.         basetypes = TREE_VALUE (basetypes);
  537.  
  538.       rhs_method = TREE_VALUE (baselink);
  539.       for (; rhs_method; rhs_method = TREE_CHAIN (rhs_method))
  540.         if (sig_mname == DECL_NAME (rhs_method)
  541.         && ! DECL_STATIC_FUNCTION_P (rhs_method)
  542.         && match_method_types (sig_mtype, TREE_TYPE (rhs_method)))
  543.           break;
  544.  
  545.       if (rhs_method == NULL_TREE
  546.           || (compute_access (basetypes, rhs_method)
  547.           != access_public))
  548.         {
  549.           error ("class `%s' does not contain a method conforming to `%s'",
  550.              TYPE_NAME_STRING (rhstype),
  551.              fndecl_as_string (NULL, sig_method, 1));
  552.           undo_casts (sig_ty);
  553.           return error_mark_node;
  554.         }
  555.     }
  556.  
  557.       if (sig_ptr_p && rhs_method != sig_method)
  558.     {
  559.       tree rhs_field = DECL_MEMFUNC_POINTER_TO (rhs_method);
  560.  
  561.       if (first_rhs_field == NULL_TREE)
  562.         {
  563.           first_rhs_field = rhs_field;
  564.           last_rhs_field = rhs_field;
  565.         }
  566.       else if (TREE_CHAIN (last_rhs_field) == rhs_field)
  567.         last_rhs_field = rhs_field;
  568.       else
  569.         offset_p = 0;
  570.       
  571.       tbl_entry = build_component_ref (rhs, DECL_NAME (rhs_field),
  572.                        NULL_TREE, 1);
  573.     }
  574.       else
  575.     {
  576.       tree code, offset, pfn;
  577.  
  578.       if (rhs_method == sig_method)
  579.         {
  580.           code = integer_two_node;
  581.           offset = integer_zero_node;
  582.           pfn = build_unary_op (ADDR_EXPR, rhs_method, 0);
  583.           TREE_TYPE (pfn) = ptr_type_node;
  584.           offset_p = 0;    /* we can't offset the rhs sig table */
  585.         }
  586.       else if (DECL_VINDEX (rhs_method))
  587.         {
  588.           code = integer_one_node;
  589.           offset = DECL_VINDEX (rhs_method);
  590.           pfn = null_pointer_node;
  591.         }
  592.       else
  593.         {
  594.           code = integer_zero_node;
  595.           offset = integer_zero_node;
  596.           pfn = build_unary_op (ADDR_EXPR, rhs_method, 0);
  597.           TREE_TYPE (pfn) = ptr_type_node;
  598.           TREE_ADDRESSABLE (rhs_method) = 1;
  599.         }
  600.  
  601.       tbl_entry = tree_cons (NULL_TREE, code,
  602.                  tree_cons (NULL_TREE, offset,
  603.                         build_tree_list (NULL_TREE, pfn)));
  604.       tbl_entry = build_nt (CONSTRUCTOR, NULL_TREE, tbl_entry);
  605.       TREE_HAS_CONSTRUCTOR (tbl_entry) = 1;
  606.       TREE_CONSTANT (tbl_entry) = 1;
  607.     }
  608.  
  609.       /* Chain those function address expressions together.  */
  610.       if (result)
  611.     result = tree_cons (NULL_TREE, tbl_entry, result);
  612.       else
  613.     result = build_tree_list (NULL_TREE, tbl_entry);
  614.     }
  615.  
  616.   if (result == NULL_TREE)
  617.     {
  618.       /* The signature was empty, we don't need a signature table.  */
  619.       undo_casts (sig_ty);
  620.       return NULL_TREE;
  621.     }
  622.  
  623.   if (offset_p)
  624.     {
  625.       if (first_rhs_field == TYPE_FIELDS (rhstype))
  626.     {
  627.       /* The sptr field on the lhs can be copied from the rhs.  */
  628.       undo_casts (sig_ty);
  629.       return integer_zero_node;
  630.     }
  631.       else
  632.     {
  633.       /* The sptr field on the lhs will point into the rhs sigtable.  */
  634.       undo_casts (sig_ty);
  635.       return build_component_ref (rhs, DECL_NAME (first_rhs_field),
  636.                       NULL_TREE, 0);
  637.     }
  638.     }
  639.  
  640.   /* We need to construct a new signature table.  */
  641.   result = build_nt (CONSTRUCTOR, NULL_TREE, nreverse (result));
  642.   TREE_HAS_CONSTRUCTOR (result) = 1;
  643.   TREE_CONSTANT (result) = !sig_ptr_p;
  644.  
  645.   undo_casts (sig_ty);
  646.   return result;
  647. }
  648.  
  649. /* Build a signature table declaration and initialize it or return an
  650.    existing one if we built one already.  If we don't get a constructor
  651.    as initialization expression, we don't need a new signature table
  652.    variable and just hand back the init expression.
  653.  
  654.    The declaration processing is done by hand instead of using `cp_finish_decl'
  655.    so that we can make signature pointers global variables instead of
  656.    static ones.  */
  657.  
  658. static tree
  659. build_sigtable (sig_type, rhs_type, init_from)
  660.      tree sig_type, rhs_type, init_from;
  661. {
  662.   tree name = NULL_TREE;
  663.   tree decl = NULL_TREE;
  664.   tree init_expr;
  665.  
  666.   push_obstacks_nochange ();
  667.   end_temporary_allocation ();
  668.  
  669.   if (! IS_SIGNATURE (rhs_type))
  670.     {
  671.       name = get_sigtable_name (sig_type, rhs_type);
  672.       decl = IDENTIFIER_GLOBAL_VALUE (name);
  673.     }
  674.   if (decl == NULL_TREE)
  675.     {
  676.       tree init;
  677.  
  678.       /* We allow only one signature table to be generated for signatures
  679.      with opaque types.  Otherwise we create a loophole in the type
  680.      system since we could cast data from one classes implementation
  681.      of the opaque type to that of another class.  */
  682.       if (SIGNATURE_HAS_OPAQUE_TYPEDECLS (sig_type)
  683.       && SIGTABLE_HAS_BEEN_GENERATED (sig_type))
  684.     {
  685.       error ("signature with opaque type implemented by multiple classes");
  686.       return error_mark_node;
  687.     }
  688.       SIGTABLE_HAS_BEEN_GENERATED (sig_type) = 1;
  689.  
  690.       init_expr = build_signature_table_constructor (sig_type, init_from);
  691.       if (init_expr == NULL_TREE || TREE_CODE (init_expr) != CONSTRUCTOR)
  692.     return init_expr;
  693.  
  694.       if (name == NULL_TREE)
  695.     name = get_sigtable_name (sig_type, rhs_type);
  696.       {
  697.     tree context = current_function_decl;
  698.  
  699.     /* Make the signature table global, not just static in whichever
  700.        function a signature pointer/ref is used for the first time.  */
  701.     current_function_decl = NULL_TREE;
  702.     decl = pushdecl_top_level (build_decl (VAR_DECL, name, sig_type));
  703.     current_function_decl = context;
  704.       }
  705.       IDENTIFIER_GLOBAL_VALUE (name) = decl;
  706.       store_init_value (decl, init_expr);
  707.       if (IS_SIGNATURE (rhs_type))
  708.     {
  709.       init = DECL_INITIAL (decl);
  710.       DECL_INITIAL (decl) = error_mark_node;
  711.     }
  712.  
  713.       DECL_ALIGN (decl) = MAX (TYPE_ALIGN (double_type_node),
  714.                    DECL_ALIGN (decl));
  715. #if 0
  716.       /* GDB-4.7 doesn't find the initialization value of a signature table
  717.      when it is constant.  */
  718.       TREE_READONLY (decl) = 1;
  719. #endif
  720.       TREE_STATIC (decl) = 1;
  721.       TREE_USED (decl) = 1;
  722.  
  723.       make_decl_rtl (decl, NULL, 1);
  724.       if (IS_SIGNATURE (rhs_type))
  725.     expand_static_init (decl, init);
  726.     }
  727.  
  728.   pop_obstacks ();
  729.  
  730.   return decl;
  731. }
  732.  
  733. /* Create a constructor or modify expression if the LHS of an assignment
  734.    is a signature pointer or a signature reference.  If LHS is a record
  735.    type node, we build a constructor, otherwise a compound expression.  */
  736.  
  737. tree
  738. build_signature_pointer_constructor (lhs, rhs)
  739.      tree lhs, rhs;
  740. {
  741.   register struct obstack *ambient_obstack = current_obstack;
  742.   register struct obstack *ambient_saveable_obstack = saveable_obstack;
  743.   int initp = (TREE_CODE (lhs) == RECORD_TYPE);
  744.   tree lhstype = initp ? lhs : TREE_TYPE (lhs);
  745.   tree rhstype = TREE_TYPE (rhs);
  746.   tree sig_ty  = SIGNATURE_TYPE (lhstype);
  747.   tree sig_tbl, sptr_expr, optr_expr, vptr_expr;
  748.   tree result;
  749.  
  750.   if (! ((TREE_CODE (rhstype) == POINTER_TYPE
  751.       && TREE_CODE (TREE_TYPE (rhstype)) == RECORD_TYPE)
  752.      || (TYPE_LANG_SPECIFIC (rhstype) &&
  753.          (IS_SIGNATURE_POINTER (rhstype)
  754.           || IS_SIGNATURE_REFERENCE (rhstype)))))
  755.     {
  756.       error ("invalid assignment to signature pointer or reference");
  757.       return error_mark_node;
  758.     }
  759.  
  760.   if (TYPE_SIZE (sig_ty) == NULL_TREE)
  761.     {
  762.       cp_error ("undefined signature `%T' used in signature %s declaration",
  763.         sig_ty,
  764.         IS_SIGNATURE_POINTER (lhstype) ? "pointer" : "reference");
  765.       return error_mark_node;
  766.     }
  767.  
  768.   /* If SIG_TY is permanent, make the signature table constructor and
  769.      the signature pointer/reference constructor permanent too.  */
  770.   if (TREE_PERMANENT (sig_ty))
  771.     {
  772.       current_obstack = &permanent_obstack;
  773.       saveable_obstack = &permanent_obstack;
  774.     }
  775.  
  776.   if (TYPE_LANG_SPECIFIC (rhstype) &&
  777.       (IS_SIGNATURE_POINTER (rhstype) || IS_SIGNATURE_REFERENCE (rhstype)))
  778.     {
  779.       if (SIGNATURE_TYPE (rhstype) == sig_ty)
  780.     {
  781.       /* LHS and RHS are signature pointers/refs of the same signature.  */
  782.       optr_expr = build_optr_ref (rhs);
  783.       sptr_expr = build_sptr_ref (rhs);
  784.       vptr_expr = build_vptr_ref (rhs);
  785.     }
  786.       else
  787.     {
  788.       /* We need to create a new signature table and copy
  789.          elements from the rhs signature table.  */
  790.       tree rhs_sptr_ref = build_sptr_ref (rhs);
  791.       tree rhs_tbl = build1 (INDIRECT_REF, SIGNATURE_TYPE (rhstype),
  792.                  rhs_sptr_ref);
  793.  
  794.       sig_tbl = build_sigtable (sig_ty, SIGNATURE_TYPE (rhstype), rhs_tbl);
  795.       if (sig_tbl == error_mark_node)
  796.         return error_mark_node;
  797.  
  798.       optr_expr = build_optr_ref (rhs);
  799.       if (sig_tbl == NULL_TREE)
  800.         /* The signature was empty.  The signature pointer is
  801.            pretty useless, but the user has been warned.  */
  802.         sptr_expr = copy_node (null_pointer_node);
  803.       else if (sig_tbl == integer_zero_node)
  804.         sptr_expr = rhs_sptr_ref;
  805.       else
  806.         sptr_expr = build_unary_op (ADDR_EXPR, sig_tbl, 0);
  807.       TREE_TYPE (sptr_expr) = build_pointer_type (sig_ty);
  808.       vptr_expr = build_vptr_ref (rhs);
  809.     }
  810.     }
  811.   else
  812.     {
  813.       tree rhs_vptr;
  814.  
  815.       if (TYPE_USES_COMPLEX_INHERITANCE (TREE_TYPE (rhstype)))
  816.     {
  817.       sorry ("class with multiple inheritance as implementation of signature");
  818.       return error_mark_node;
  819.     }
  820.  
  821.       sig_tbl = build_sigtable (sig_ty, TREE_TYPE (rhstype), rhs);
  822.       if (sig_tbl == error_mark_node)
  823.     return error_mark_node;
  824.  
  825.       optr_expr = rhs;
  826.       if (sig_tbl == NULL_TREE)
  827.     /* The signature was empty.  The signature pointer is
  828.        pretty useless, but the user has been warned.  */
  829.     {
  830.       sptr_expr = copy_node (null_pointer_node);
  831.       TREE_TYPE (sptr_expr) = build_pointer_type (sig_ty);
  832.     }
  833.       else
  834.     sptr_expr = build_unary_op (ADDR_EXPR, sig_tbl, 0);
  835.       if (CLASSTYPE_VFIELD (TREE_TYPE (rhstype)))
  836.     {
  837.       rhs_vptr = DECL_NAME (CLASSTYPE_VFIELD (TREE_TYPE (rhstype)));
  838.       vptr_expr = build_component_ref (build_indirect_ref (rhs, 0),
  839.                        rhs_vptr, NULL_TREE, 0);
  840.     }
  841.       else
  842.     vptr_expr = copy_node (null_pointer_node);
  843.       TREE_TYPE (vptr_expr) = build_pointer_type (vtbl_type_node);
  844.     }
  845.  
  846.   if (initp)
  847.     {
  848.       result = tree_cons (NULL_TREE, optr_expr,
  849.               tree_cons (NULL_TREE, sptr_expr,
  850.                      build_tree_list (NULL_TREE, vptr_expr)));
  851.       result = build_nt (CONSTRUCTOR, NULL_TREE, result);
  852.       TREE_HAS_CONSTRUCTOR (result) = 1;
  853.       result = digest_init (lhstype, result, 0);
  854.     }
  855.   else
  856.     {
  857.       if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype))
  858.       readonly_error (lhs, "assignment", 0);
  859.  
  860.       optr_expr = build_modify_expr (build_optr_ref (lhs), NOP_EXPR,
  861.                      optr_expr);
  862.       sptr_expr = build_modify_expr (build_sptr_ref (lhs), NOP_EXPR,
  863.                      sptr_expr);
  864.       vptr_expr = build_modify_expr (build_vptr_ref (lhs), NOP_EXPR,
  865.                      vptr_expr);
  866.  
  867.       result = tree_cons (NULL_TREE, optr_expr,
  868.               tree_cons (NULL_TREE, sptr_expr,
  869.                      tree_cons (NULL_TREE, vptr_expr,
  870.                         build_tree_list (NULL_TREE,
  871.                                  lhs))));
  872.       result = build_compound_expr (result);
  873.     }
  874.  
  875.   current_obstack = ambient_obstack;
  876.   saveable_obstack = ambient_saveable_obstack;
  877.   return result;
  878. }
  879.  
  880. /* Build a temporary variable declaration for the instance of a signature
  881.    member function call if it isn't a declaration node already.  Simply
  882.    using a SAVE_EXPR doesn't work since we need `this' in both branches
  883.    of a conditional expression.  */
  884.  
  885. static tree
  886. save_this (instance)
  887.      tree instance;
  888. {
  889.   tree decl;
  890.  
  891.   if (TREE_CODE_CLASS (TREE_CODE (instance)) == 'd')
  892.     decl = instance;
  893.   else
  894.     {
  895.       decl = build_decl (VAR_DECL, NULL_TREE, TREE_TYPE (instance));
  896.       DECL_REGISTER (decl) = 1;
  897.       layout_decl (decl, 0);
  898.       expand_decl (decl);
  899.     }
  900.  
  901.   return decl;
  902. }
  903.  
  904. /* Build a signature member function call.  Looks up the signature table
  905.    entry corresponding to FUNCTION.  Depending on the value of the CODE
  906.    field, either call the function in PFN directly, or use OFFSET to
  907.    index INSTANCE's virtual function table.  */
  908.  
  909. tree
  910. build_signature_method_call (basetype, instance, function, parms)
  911.      tree basetype, instance, function, parms;
  912. {
  913.   tree saved_instance = save_this (instance);    /* Create temp for `this'.  */
  914.   tree signature_tbl_ptr = build_sptr_ref (saved_instance);
  915.   tree sig_field_name = DECL_NAME (DECL_MEMFUNC_POINTER_TO (function));
  916.   tree basetype_path = TYPE_BINFO (basetype);
  917.   tree tbl_entry = build_component_ref (build1 (INDIRECT_REF, basetype,
  918.                         signature_tbl_ptr),
  919.                     sig_field_name, basetype_path, 1);
  920.   tree code, offset, pfn, vfn;
  921.   tree deflt_call = NULL_TREE, direct_call, virtual_call, result;
  922.  
  923.   code = build_component_ref (tbl_entry, get_identifier (SIGTABLE_CODE_NAME),
  924.                  NULL_TREE, 1);
  925.   offset = build_component_ref (tbl_entry,
  926.                 get_identifier (SIGTABLE_OFFSET_NAME),
  927.                  NULL_TREE, 1);
  928.   pfn = build_component_ref (tbl_entry, get_identifier (SIGTABLE_PFN_NAME),
  929.                  NULL_TREE, 1);
  930.   TREE_TYPE (pfn) = build_pointer_type (TREE_TYPE (function)); 
  931.  
  932.   if (IS_DEFAULT_IMPLEMENTATION (function))
  933.     {
  934.       pfn = save_expr (pfn);
  935.       deflt_call = build_function_call (pfn,
  936.                     tree_cons (NULL_TREE, saved_instance,
  937.                            TREE_CHAIN (parms)));
  938.     }
  939.  
  940.   {
  941.     /* Cast the signature method to have `this' of a normal pointer type.  */
  942.     tree old_this = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (TREE_TYPE (pfn))));
  943.  
  944.     TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (TREE_TYPE (pfn)))) =
  945.       build_type_variant (TYPE_POINTER_TO (basetype),
  946.               TYPE_READONLY (old_this),
  947.               TYPE_VOLATILE (old_this));
  948.  
  949.     direct_call = build_function_call (pfn, parms);
  950.  
  951.     vfn = build_vfn_ref (&TREE_VALUE (parms), saved_instance, offset);
  952.     TREE_TYPE (vfn) = build_pointer_type (TREE_TYPE (function));
  953.     virtual_call = build_function_call (vfn, parms);
  954.  
  955.     /* Undo the cast, make `this' a signature pointer again.  */
  956.     TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (TREE_TYPE (pfn)))) = old_this;
  957.   }
  958.  
  959.   /* Once the function was found, there should be no reason why we
  960.      couldn't build the member function pointer call.  */
  961.   if (!direct_call || direct_call == error_mark_node
  962.       || !virtual_call || virtual_call == error_mark_node
  963.       || (IS_DEFAULT_IMPLEMENTATION (function)
  964.       && (!deflt_call || deflt_call == error_mark_node)))
  965.     {
  966.       compiler_error ("cannot build call of signature member function `%s'",
  967.               fndecl_as_string (NULL, function, 1));
  968.       return error_mark_node;
  969.     }
  970.  
  971.   if (IS_DEFAULT_IMPLEMENTATION (function))
  972.     {
  973.       tree test = build_binary_op_nodefault (EQ_EXPR, code, integer_one_node,
  974.                          EQ_EXPR);
  975.       result = build_conditional_expr (code,
  976.                        build_conditional_expr (test,
  977.                                    virtual_call,
  978.                                    deflt_call),
  979.                        direct_call);
  980.     }
  981.   else
  982.     result = build_conditional_expr (code, virtual_call, direct_call);
  983.  
  984.   /* If we created a temporary variable for `this', initialize it first.  */
  985.   if (instance != saved_instance)
  986.     result = build (COMPOUND_EXPR, TREE_TYPE (result),
  987.             build_modify_expr (saved_instance, NOP_EXPR, instance),
  988.             result);
  989.  
  990.   return result;
  991. }
  992.  
  993. /* Create a COMPONENT_REF expression for referencing the OPTR field
  994.    of a signature pointer or reference.  */
  995.  
  996. tree
  997. build_optr_ref (instance)
  998.      tree instance;
  999. {
  1000.   tree field = get_identifier (SIGNATURE_OPTR_NAME);
  1001.  
  1002.   return build_component_ref (instance, field, NULL_TREE, 1);
  1003. }
  1004.  
  1005. /* Create a COMPONENT_REF expression for referencing the SPTR field
  1006.    of a signature pointer or reference.  */
  1007.  
  1008. tree
  1009. build_sptr_ref (instance)
  1010.      tree instance;
  1011. {
  1012.   tree field = get_identifier (SIGNATURE_SPTR_NAME);
  1013.  
  1014.   return build_component_ref (instance, field, NULL_TREE, 1);
  1015. }
  1016.  
  1017. /* Create a COMPONENT_REF expression for referencing the VPTR field
  1018.    of a signature pointer or reference.  */
  1019.  
  1020. tree
  1021. build_vptr_ref (instance)
  1022.      tree instance;
  1023. {
  1024.   tree field = get_identifier (SIGNATURE_VPTR_NAME);
  1025.  
  1026.   return build_component_ref (instance, field, NULL_TREE, 1);
  1027. }
  1028.